﻿using System;
using TestRunner.App.Base;
using TestRunner.App.Common;
using TestRunner.App.Model;
using GalaSoft.MvvmLight.Command;
using TestRunner.App.Entities;
using SD.Libs.Navigation.Contracts;

namespace TestRunner.App.ViewModels
{
    public class CustomerDetailVM : MyVMBase
    {
        #region Fields

        private IModel _model;
        INavigationService _navigationService;

        #endregion Fields

        #region Commands

        private RelayCommand _navigateToCustomerListCommand;
        public RelayCommand NavigateToCustomerListCommand
        {
            get
            {
                if (_navigateToCustomerListCommand == null)
                {
                    _navigateToCustomerListCommand = new RelayCommand(() =>
                    {
                        string url = NavigationHelper.CustomerListURL();
                        _navigationService.NavigateTo(new Uri(url, UriKind.Relative));
                    },
                                                                        null);


                }

                return _navigateToCustomerListCommand;
            }
        }

        #endregion

        #region Properties for bindings

        Customer _currentCustomer;
        public Customer CurrentCustomer
        {
            get
            {
                return _currentCustomer;
            }

            set
            {
                _currentCustomer = value;
                RaisePropertyChanged("CurrentCustomer");

            }
        }

        #endregion Properties for bindings

        #region Constructor

        public CustomerDetailVM(INavigationService navigationService, IModel model)
        {
            _model = model;
            _navigationService = navigationService;
            Navigated += CustomerDetailVM_Navigated;
            if (IsInDesignMode)
                CurrentCustomer = _model.GetCustomer(0);
        }

        #endregion

        #region events

        void CustomerDetailVM_Navigated(object sender, NavigationContextEventArgs e)
        {
            var paramString = e.QueryString[NavigationHelper.CustomerDetailVMParams.idParam];
            CurrentCustomer = _model.GetCustomer(Convert.ToInt32(paramString));
        }

        #endregion
    }
}
